class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* pre = new ListNode(0, head); 
        ListNode* last = pre;    
        ListNode* node = head;    
        int sameVal;    
        int count;      
        while(node != nullptr){
            sameVal = node->val; 
            count = 1;  
            while(node->next != nullptr && node->next->val == sameVal){
                
                node = node->next;   
                count++;
            }
            if(count == 1){
                last = node;    
            }else{
                last->next = node->next;  
            }
            node = node->next;   
        }
        return pre->next;
    }
};